package com.yugao.fintech.draper.core.model;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;

@Data
@Accessors(chain = true)
@NoArgsConstructor
public class ResultVO<T> implements Serializable {

    private static final long serialVersionUID = 3194875321804368471L;

    /**
     * 响应状态码
     */
    private Integer code;

    /**
     * 描述信息
     */
    private String msg;

    /**
     * 数据
     */
    private T data;

    public ResultVO(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResultVO(ErrorCode errorCode) {
        this.code = errorCode.getCode();
        this.msg = errorCode.getMsg();
    }

    public static <T> ResultVO<T> success() {
        return new ResultVO<T>(ErrorCode.SUCCESS.getCode(), ErrorCode.SUCCESS.getMsg(), null);
    }

    public static <T> ResultVO<T> success(T data) {
        return new ResultVO<T>(ErrorCode.SUCCESS.getCode(), ErrorCode.SUCCESS.getMsg(), data);
    }

    public static <T> ResultVO<T> success(ErrorCode errorCode, T data) {
        return new ResultVO<T>(errorCode.getCode(), errorCode.getMsg(), data);
    }

    public static <T> ResultVO<T> error() {
        return new ResultVO<T>(ErrorCode.SYSTEM_ERROR.getCode(), ErrorCode.SYSTEM_ERROR.getMsg(), null);
    }

    public static <T> ResultVO<T> error(ErrorCode code, String msg) {
        return new ResultVO<T>(code.getCode(), msg, null);
    }

    public static <T> ResultVO<T> error(String msg) {
        return new ResultVO<>(ErrorCode.SYSTEM_ERROR.getCode(), msg, null);
    }

    public static <T> ResultVO<T> error(Integer code, String msg) {
        return new ResultVO<>(code, msg, null);
    }


    public static <T> ResultVO<T> judge(boolean status) {
        return status ? success() : error();
    }

    public static <T> ResultVO result(ErrorCode resultCode, T data) {
        return result(resultCode.getCode(), resultCode.getMsg(), data);
    }

    public static <T> ResultVO<T> result(Integer code, String msg, T data) {
        return new ResultVO<T>().setCode(code).setData(data).setMsg(msg);
    }

    public static boolean isSuccess(ResultVO<?> result) {
        return result != null && ErrorCode.SUCCESS.getCode().equals(result.getCode());
    }
}
